package com.qqtech.qquan.circle.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.model.CircleMember;
import com.qqtech.qquan.circle.model.CircleSign;
import com.qqtech.qquan.circle.model.CircleSignQuery;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleMemberService;
import com.qqtech.qquan.circle.service.CircleSignService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/circle/sign")
public class CircleSignController extends BaseWebController<CircleSign, CircleSignQuery> {
	@Resource
	private CircleSignService circleSignService;
	@Resource
	private CircleBaseService circleBaseService;
	@Resource
	private CircleMemberService circleMemberService;
	
	@Override
	protected BaseService<CircleSign> getBaseService() {
		return circleSignService;
	}
	
	/**
	 * 签到
	 * @param circleMember
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/sign")
	@ResponseBody
	public Result sign(@RequestBody CircleMember circleMember){
		Result result = new Result();
		if(circleMember==null){
			result.addErr("成员信息为空！");
			return result;
		}
		Integer curUserId = super.getUserId();//当前用户id
		Integer circleId =circleMember.getCircleId();
		CircleBaseVo circleVo = circleBaseService.getById(circleId);
		if(circleVo==null){
			result.addErr("找不到该圈子！");
			return result;
		}
		if(!circleMemberService.isCircleMember(curUserId, circleId)){
			result.addErr("你还没有加入这个圈子！");
			return result;
		}
		if(circleSignService.isSignToday(curUserId,circleId)){
			result.addErr("你今天已经签到！");
			return result;
		}
		circleSignService.handleSign(curUserId, circleId);
		return result;
	}
}
